<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="昵称" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入昵称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="真实名字" prop="realName">
        <el-input v-model="queryParams.realName" placeholder="请输入真实名字" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="角色" prop="frentLink">
        <el-select v-model="queryParams.frentLink">
          <el-option value="0" label="会员"></el-option>
          <el-option value="1" label="超级会员"></el-option>
          <el-option value="2" label="普通用户"></el-option>
          <el-option value="3" label="友链管理员"></el-option>
          <el-option value="5" label="骗审用户"></el-option>
          <el-option value="30" label="会员/友链管理员"></el-option>
          <el-option value="31" label="超级会员/友链管理员"></el-option>
          <el-option value="32" label="普通用户/友链管理员"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['pauser:pauser:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['pauser:pauser:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['pauser:pauser:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pauserList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="头像" align="center" prop="picUrl">
        <template slot-scope="scope">
          <image-preview :src="scope.row.picUrl" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{ scope.row.status === 0 ? "正常" : "禁用" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="角色" align="center" prop="frentLink">
        <template slot-scope="scope">
          <span v-if="scope.row.frentLink === 0">会员</span>
          <span v-if="scope.row.frentLink === 1">超级会员</span>
          <span v-if="scope.row.frentLink === 2">普通用户</span>
          <span v-if="scope.row.frentLink === 3">友链管理员</span>
          <span v-if="scope.row.frentLink === 5">骗审用户</span>
          <span v-if="scope.row.frentLink === 30">会员/友链</span>
          <span v-if="scope.row.frentLink === 31">超级会员/友链</span>
          <span v-if="scope.row.frentLink === 32">普通用户/友链</span>
        </template>
      </el-table-column>
    
      <el-table-column label="真实名字" align="center" prop="realName" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="生日" align="center" prop="birth" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birth, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="密码" align="center" prop="passWord" />
      <el-table-column label="标签" align="center" prop="userTab" >
        <template slot-scope="scope">
          <el-popover placement="bottom" width="800" trigger="click" :content="userTab">
            <el-button size="medium" type="text" icon="el-icon-view" slot="reference"
              @click="openNodeHtml(scope.row.userTab)"></el-button>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['pauser:pauser:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['pauser:pauser:remove']">删除</el-button>
            <el-button size="mini" type="text" icon="el-icon-document" @click="account(scope.row)">账单</el-button>
            <el-button size="mini" type="text" icon="el-icon-picture-outline" @click="pic(scope.row)">相册</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改用户管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="头像" prop="picUrl">
          <el-image :src="form.picUrl" fit="cover" :style="`width:120px;height:120px;`" :preview-src-list="realSrcList">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status">
            <el-option :value="0" label="正常"></el-option>
            <el-option :value="1" label="禁用"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色" prop="frentLink">
          <el-select v-model="form.frentLink">
            <el-option :value="0" label="会员"></el-option>
            <el-option :value="1" label="超级会员"></el-option>
            <el-option :value="2" label="普通用户"></el-option>
            <el-option :value="5" label="骗审用户"></el-option>
            <el-option :value="3" label="友链管理员"></el-option>
            <el-option :value="30" label="会员/友链管理员"></el-option>
            <el-option :value="31" label="超级会员/友链管理员"></el-option>
            <el-option :value="32" label="普通用户/友链管理员"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="日期" prop="birth">
          <el-date-picker clearable v-model="form.birth" type="date" value-format="yyyy-MM-dd" placeholder="请选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="真实名字" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实名字" />
        </el-form-item>
        <el-form-item label="标签" prop="userTab">
          <el-input v-model="form.userTab" placeholder="请输入标签" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPauser, getPauser, delPauser, addPauser, updatePauser } from "@/api/pauser/pauser";

export default {
  name: "Pauser",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户管理表格数据
      pauserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nickName: null,
        picUrl: null,
        status: null,
        phone: null,
        openId: null,
        email: null,
        passWord: null,
        sessionKey: null,
        realName: null,
        frentLink: null,
        userTab: null,
        birth: null
      },
      userTab:'',
      visible:false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        openId: [
          { required: true, message: "用户openid不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    account(item){
      this.$router.push({
        path: '/picture/account/paccount',
        query: {
          id: item.id,
        }
      })
    },
    pic(item){
      this.$router.push({
        path: '/picture/acpictures',
        query: {
          id: item.id,
        }
      })
    },
    openNodeHtml(userTab) {
      this.userTab = userTab.replace(/<[^>]+>/g, "");
      this.visible = true
    },
    /** 查询用户管理列表 */
    getList() {
      this.loading = true;
      listPauser(this.queryParams).then(response => {
        this.pauserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        nickName: null,
        picUrl: null,
        status: null,
        phone: null,
        openId: null,
        email: null,
        passWord: null,
        sessionKey: null,
        realName: null,
        frentLink: null,
        userTab: null,
        birth: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPauser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePauser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPauser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除用户管理编号为"' + ids + '"的数据项？').then(function () {
        return delPauser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pauser/pauser/export', {
        ...this.queryParams
      }, `pauser_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
